package com.hy.mydesktop.client.component.factory;






import java.util.List;
import java.util.Map;

import com.extjs.gxt.desktop.client.Desktop;
import com.extjs.gxt.desktop.client.Shortcut;
import com.extjs.gxt.ui.client.Style.ButtonScale;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.event.MenuEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;

import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.hy.mydesktop.client.component.event.AppEvents;
import com.hy.mydesktop.client.component.meta.ButtonModelEnum;
import com.hy.mydesktop.client.component.meta.ShortcutModelEnum;
import com.hy.mydesktop.client.component.mvc.controller.meta.WindowControllerModelEnum;
import com.hy.mydesktop.client.component.mvc.event.EventStructureMetaModel;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;




/**
 * 
 * <ul>
 * <li>开发作者：花宏宇</li>
 * <li>设计日期：2010-9-9；时间：下午01:09:41</li>
 * <li>类型名称：ShortcutFactory</li>
 * <li>设计目的：ButtonFactory用于创建按钮，并且根据GxtComponentMetaModel的信息，
 * 对按钮进行个性化配置</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class ShortcutFactory {
	public static Desktop createShortcut(Desktop desktop,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
		
		//判断是哪一类的按钮
		//每一个的按钮如何实现？decorate？
		//根据数据库的buttton的配置信息，如何实现decorate，decorate时有无先后顺序
		return createBaseShortcut(desktop,gxtComponentMetaNodeModel);
		
		
	}
	

	
	/**
	 * 依据GxtComponentMetaModel对按钮，进行个性化配置
	 * @param gxtComponentMetaModel
	 * @return
	 */
	private static Desktop createBaseShortcut(final Desktop desktop,GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {
		
		Shortcut shortcut  = createDefaultShortcut();
	
		//进行个性化配置
		//String title =(String)gxtComponentMetaModel.get(ButtonModelEnum.TITLE.name().toLowerCase());
		if (gxtComponentMetaNodeModel.get(ShortcutModelEnum.TEXT.name().toLowerCase())!=null) {
			//设置Button的title
			 //= (String)gxtComponentMetaModel.get(ButtonModelEnum.TITLE.name().toLowerCase());
			shortcut.setText((String)gxtComponentMetaNodeModel.get(ShortcutModelEnum.TEXT.name().toLowerCase()));
			
		}if (gxtComponentMetaNodeModel.get(ShortcutModelEnum.TITLE.name().toLowerCase())!=null) {
			
			shortcut.setTitle((String)gxtComponentMetaNodeModel.get(ShortcutModelEnum.TITLE.name().toLowerCase()));
		}if (gxtComponentMetaNodeModel.get(ShortcutModelEnum.ID.name().toLowerCase())!=null) {
			
			shortcut.setId((String)gxtComponentMetaNodeModel.get(ShortcutModelEnum.ID.name().toLowerCase()));
		}if (gxtComponentMetaNodeModel.get(ShortcutModelEnum.ICON.name().toLowerCase())!=null) {
			
			shortcut.setIconStyle((String)gxtComponentMetaNodeModel.get(ShortcutModelEnum.ICON.name().toLowerCase()));
		}if (gxtComponentMetaNodeModel.get(ShortcutModelEnum.DATA.name().toLowerCase())!=null) {
			//TODO short.setData???
			//shortcut.setText((String)gxtComponentMetaNodeModel.get(ShortcutModelEnum.TITLE.name().toLowerCase()));
		}
		if(gxtComponentMetaNodeModel.get(ShortcutModelEnum.REGISTEREVENTTYPES.name().toLowerCase())!=null){
					
			final Map<String,EventStructureMetaModel> appEventCodes = gxtComponentMetaNodeModel.get(ShortcutModelEnum.REGISTEREVENTTYPES.name().toLowerCase());
			for ( final String code : appEventCodes.keySet()) {
				Integer i = Integer.valueOf(code);
				switch (i) {
		            case 1://Integer.valueOf("0001")
		            	//shortcutController.addRegisterEventType(AppEvents.Login);
		            	
		                break;
		            case 2://Integer.valueOf("0002")
		            	//shortcutController.addRegisterEventType(AppEvents.DESKTOP_INIT_FOR_LOGGED_IN_USER);	
		                break;
		            case 3://Integer.valueOf("0003")
		            	SelectionListener<ComponentEvent> shortcutListener = new SelectionListener<ComponentEvent>() {
						      @Override
						      public void componentSelected(ComponentEvent ce) {
						    	 //itemSelected(desktop,ce);
						    	  //TODO 如何传递数据伴随Appeve事件的数据？
						    	  Map<String, Object> data = (Map<String, Object>)appEventCodes.get(code);
						    	  
						    	  EventStructureMetaModel eventstructuremetamodel = 
										 (EventStructureMetaModel)data.get("eventstructuremetamodel");
						    	  System.out.println("Shortcut触发AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT事件:"
						    			  +"控制器的Controller Id 是"+eventstructuremetamodel.getControllerId()
						    			  +""+eventstructuremetamodel.getResponseModeId());
						    	  Dispatcher.forwardEvent(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT,data);
								 
					    }};
					    /**
						 * 桌面快捷方式的监听器
						 */
					    shortcut.addSelectionListener(shortcutListener);
//						Dispatcher.forwardEvent(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT, data);
						//TODO 在抛出事件时，如何传输APPEVENT需要的data ？？
		                break;
		            case 101://Integer.valueOf("0101")
		            	//shortcutController.addRegisterEventType(AppEvents.WINDOW_ININT);
		                break;
	           		           
		            default:
		                break;
		        }
			}
			/*List<String> appEventCodes = gxtComponentMetaNodeModel.get(ShortcutModelEnum.REGISTEREVENTTYPES.name().toLowerCase());
			for (String code : appEventCodes) {
				Integer i = Integer.valueOf(code);
				switch (i) {
		            case 1://Integer.valueOf("0001")
		            	//shortcutController.addRegisterEventType(AppEvents.Login);
		            	
		                break;
		            case 2://Integer.valueOf("0002")
		            	//shortcutController.addRegisterEventType(AppEvents.DESKTOP_INIT_FOR_LOGGED_IN_USER);	
		                break;
		            case 3://Integer.valueOf("0003")
		            	SelectionListener<ComponentEvent> shortcutListener = new SelectionListener<ComponentEvent>() {
						      @Override
						      public void componentSelected(ComponentEvent ce) {
						    	 //itemSelected(desktop,ce);
						    	  //TODO 如何传递数据伴随Appeve事件的数据？
						    	  Dispatcher.forwardEvent(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT);
								 
					    }};
					    *//**
						 * 桌面快捷方式的监听器
						 *//*
					    shortcut.addSelectionListener(shortcutListener);
//						Dispatcher.forwardEvent(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT, data);
						//TODO 在抛出事件时，如何传输APPEVENT需要的data ？？
		                break;
		            case 101://Integer.valueOf("0101")
		            	//shortcutController.addRegisterEventType(AppEvents.WINDOW_ININT);
		                break;
	           		           
		            default:
		                break;
		        }
			}		*/
		
		}
			/*if (gxtComponentMetaNodeModel.get(ShortcutModelEnum.APPEVENT.name().toLowerCase())!=null) {
			*//**
			 * 把所有的AppEvent事件类型，映射成 特定的编码
			 * AppEvent事件编码，是否一定与控件类别是一一对应的？？？？
			 * 如果不是
			 * 0101 shortcut
			 *//*
			String appEventCode = (String)gxtComponentMetaNodeModel.get(ShortcutModelEnum.EVENTTYPE.name().toLowerCase());
			final Object data = gxtComponentMetaNodeModel.get(ShortcutModelEnum.EVENTDATA.name().toLowerCase());
			
			int appEventCodeValue = Integer.valueOf(appEventCode);
			//shortcut.setText((String)gxtComponentMetaNodeModel.get(ShortcutModelEnum.TITLE.name().toLowerCase()));
			switch (appEventCodeValue) {
				//0001
				case 1:{
					
					break;

				}//0002
				case 2:{
					
					break;

				}
				//0003 AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT
				case 3:{
					
					SelectionListener<ComponentEvent> shortcutListener = new SelectionListener<ComponentEvent>() {
					      @Override
					      public void componentSelected(ComponentEvent ce) {
					    	 //itemSelected(desktop,ce);
					    	  Dispatcher.forwardEvent(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT,data);
							 
				    }};
				    *//**
					 * 桌面快捷方式的监听器
					 *//*
				    shortcut.addSelectionListener(shortcutListener);
//					Dispatcher.forwardEvent(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT, data);
					//TODO 在抛出事件时，如何传输APPEVENT需要的data ？？
					
					
					break;

				}default:
					break;
			}
			
			desktop.addShortcut(shortcut);
				
			
		}
		*/
		
		/**
		 * 桌面快捷方式的监听器
		 */
		/*SelectionListener<ComponentEvent> shortcutListener = new SelectionListener<ComponentEvent>() {
		      @Override
		      public void componentSelected(ComponentEvent ce) {
		        itemSelected(desktop,ce);
	    }};
		shortcut.addSelectionListener(shortcutListener);*/
		//TODO short.setData???
		desktop.addShortcut(shortcut);
		return desktop;
		
	}
	
	/**
	 * DefaultButton的基本UI效果
	 * 按钮显示的文字是“确定”
	 * 按钮中等大小
	 * @return
	 */
	private static Shortcut createDefaultShortcut() {
		
		 Shortcut s1 = new Shortcut();
		    s1.setText("xxxx Shortcut");
		    s1.setId("grid-win-shortcut");
		    s1.setTitle("new Shortcut");
		    //s1.setData("window", gridWindow);
		   // s1.addSelectionListener(shortcutListener);
		    return s1;
	}
	
	/**
	 * 
	 * <ul>
	 * <li>方法含义：用来响应桌面的快捷方式的单击事件：模拟视窗系统中，窗口的所以显示效果</li>
	 * <li>方法作者：花宏宇</li>
	 * <li>编写日期：2010-9-8；时间：下午下午03:32:05</li>
	 * </ul>
	 * <ul>
	 * <b>修订编号：</b>
	 * <li>修订日期：</li>
	 * <li>修订作者：</li>
	 * <li>修订原因：</li>
	 * <li>修订内容：</li>
	 * </ul>
	 * @param ce
	 */
	 private static void itemSelected(Desktop desktop,ComponentEvent ce) {
		    Window w;
		    if (ce instanceof MenuEvent) {
		      MenuEvent me = (MenuEvent) ce;
		      //s1.setData("window", gridWindow);
		      //创建并加载窗口对象
		      w = me.getItem().getData("window");
		    } else {
		    	//s1.setData("window", gridWindow);
			      //创建并加载窗口对象
		      w = ce.getComponent().getData("window");
		    }
		    if (!desktop.getWindows().contains(w)) {
		      desktop.addWindow(w);
		    }
		  //如果w 存在（不为空）且 w是不可见到的
		    if (w != null && !w.isVisible()) {
		    	//w显示
		      w.show();
		    } else {
		    	//w最前面显示
		      w.toFront();
		    }
		  }
	 
	 public static Shortcut createShortcutToDesktop(Desktop desktop,GxtComponentMetaNodeModel gxtComponentMetaNodeModel){
			
			Shortcut shortcut  = createDefaultShortcut();
			shortcut = AbstractComponetFactory.configureAbstractComponet(shortcut, gxtComponentMetaNodeModel);
			
			//进行个性化配置
			//String title =(String)gxtComponentMetaModel.get(ButtonModelEnum.TITLE.name().toLowerCase());
			if (gxtComponentMetaNodeModel.get(ShortcutModelEnum.TEXT.name().toLowerCase())!=null) {
				//设置Button的title
				 //= (String)gxtComponentMetaModel.get(ButtonModelEnum.TITLE.name().toLowerCase());
				shortcut.setText((String)gxtComponentMetaNodeModel.get(ShortcutModelEnum.TEXT.name().toLowerCase()));
				
			}if (gxtComponentMetaNodeModel.get(ShortcutModelEnum.TITLE.name().toLowerCase())!=null) {
				
				shortcut.setTitle((String)gxtComponentMetaNodeModel.get(ShortcutModelEnum.TITLE.name().toLowerCase()));
			}if (gxtComponentMetaNodeModel.get(ShortcutModelEnum.ID.name().toLowerCase())!=null) {
				
				shortcut.setId((String)gxtComponentMetaNodeModel.get(ShortcutModelEnum.ID.name().toLowerCase()));
			}
			//设置快捷方式图标
			if (gxtComponentMetaNodeModel.get(ShortcutModelEnum.ICON.name().toLowerCase())!=null) {
				
				shortcut.setIconStyle((String)gxtComponentMetaNodeModel.get(ShortcutModelEnum.ICON.name().toLowerCase()));
			}if (gxtComponentMetaNodeModel.get(ShortcutModelEnum.DATA.name().toLowerCase())!=null) {
				//TODO short.setData???
				//shortcut.setText((String)gxtComponentMetaNodeModel.get(ShortcutModelEnum.TITLE.name().toLowerCase()));
			}
			
			
			//REGISTEREVENTTYPES : 把某一类事件（AppEvent事件），注册到控件本身，与控件本身的某一个事件，具体关联起来。
			//当控件本身的事件，被触发后，则向控制器发送AppEvent事件
			
			if(gxtComponentMetaNodeModel.get(ShortcutModelEnum.REGISTEREVENTTYPES.name().toLowerCase())!=null){
						
				final Map<String,Object> appEventCodes = gxtComponentMetaNodeModel.get(ShortcutModelEnum.REGISTEREVENTTYPES.name().toLowerCase());
				for ( final String code : appEventCodes.keySet()) {
					Integer i = Integer.valueOf(code);
					switch (i) {
			            case 1://Integer.valueOf("0001")
			            	//shortcutController.addRegisterEventType(AppEvents.Login);
			            	
			                break;
			            case 2://Integer.valueOf("0002")
			            	//shortcutController.addRegisterEventType(AppEvents.DESKTOP_INIT_FOR_LOGGED_IN_USER);	
			                break;
			            case 3://Integer.valueOf("0003")
			            	SelectionListener<ComponentEvent> shortcutListener = new SelectionListener<ComponentEvent>() {
							      @Override
							      public void componentSelected(ComponentEvent ce) {
							    	 //itemSelected(desktop,ce);
							    	  //
							    	  Map<String, Object> data = (Map<String, Object>)appEventCodes.get(code);
							    	  EventStructureMetaModel eventstructuremetamodel = 
											 (EventStructureMetaModel)data.get("eventstructuremetamodel");
							    	  System.out.println("Shortcut触发AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT事件:"
							    			  +"控制器的Controller Id 是"+eventstructuremetamodel.getControllerId()
							    			  +""+eventstructuremetamodel.getResponseModeId());
							    	  Dispatcher.forwardEvent(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT,data);
							    	  //TODO 互斥事件
							    	  /**
							    	   * 通过一个途径，获得一个boolean标记，判断到底触发哪个事件
							    	   */
									 
						    }};
						    /**
							 * 桌面快捷方式的监听器
							 */
						    shortcut.addSelectionListener(shortcutListener);
//							Dispatcher.forwardEvent(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT, data);
							//TODO 在抛出事件时，如何传输APPEVENT需要的data ？？
			                break;
			            case 4://Integer.valueOf("0003")
			            	SelectionListener<ComponentEvent> shortcutListener2 = new SelectionListener<ComponentEvent>() {
							      @Override
							      public void componentSelected(ComponentEvent ce) {
							    	 //itemSelected(desktop,ce);
							    	  //
							    	  Map<String, Object> data = (Map<String, Object>)appEventCodes.get(code);
							    	  EventStructureMetaModel eventstructuremetamodel = 
											 (EventStructureMetaModel)data.get("eventstructuremetamodel");
							    	  System.out.println("Shortcut触发AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT事件:"
							    			  +eventstructuremetamodel.getControllerId()
							    			  +eventstructuremetamodel.getResponseModeId());
							    	  Dispatcher.forwardEvent(AppEvents.DESKTOP_SHORTCUT_ONCLICK,data);
									 
						    }};
						    /**
							 * 桌面快捷方式的监听器
							 */
						    shortcut.addSelectionListener(shortcutListener2);
//							Dispatcher.forwardEvent(AppEvents.DESKTOP_SHORTCUT_ONCLICK_INIT, data);
							//TODO 在抛出事件时，如何传输APPEVENT需要的data ？？
			                break;
			            case 101://Integer.valueOf("0101")
			            	//shortcutController.addRegisterEventType(AppEvents.WINDOW_ININT);
			                break;
		           		           
			            default:
			                break;
			        }
				}
				
			
			}
				
			
			desktop.addShortcut(shortcut);
			return shortcut;
				
				
			}
}
